package com.hxqc.basic.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.hxqc.basic.serivce.MenuService;

@RestController
@RequestMapping("/menu")
public class MenuController {

	@Resource
	private MenuService menuService;
	
	/**
	 * 菜单列表
	 * @param params
	 * @return  
	 */
	@RequestMapping("/list")
	public Map<String, Object> list(@RequestParam Map<String, String> params) {
		Integer repartCount = menuService.countSortIdRepart();
		if(null!=repartCount&&repartCount>0){
			menuService.initMenuSorting();
		}
		
		int count = menuService.countAllRights(params);
		List<Map<String, Object>> data = menuService.findAllRights(params);
		Map<String, Object> result = new HashMap<>();
		result.put("iTotalRecords", count);
		result.put("iTotalDisplayRecords", count);
		result.put("aaData", data);
		return result;
	}
	
	/**
	 * 菜单排序
	 * @param option
	 * @param right_id
	 * @return
	 */
	@RequestMapping("/order")
	public Map<String, String> order(String option, String right_id) {
		Map<String, String> result = new HashMap<String, String>();
		try {
			menuService.orderMenu(option, right_id);
			result.put("optFlag", "Y");
		} catch (Exception e) {
			e.printStackTrace();
			result.put("optFlag", "E");
			result.put("optDesc", "系统错误,请稍后重试!");
		}
		return result;
	}
	
	/**
	 * 更新排序值
	 * @param sort_id
	 * @param right_id
	 * @return
	 */
	@RequestMapping("/updateSort")
	public Map<String, String> updateSort(Integer sort_id, String right_id) {
		Map<String, String> result = new HashMap<String, String>();
		try {
			menuService.updateRightOrder(sort_id, right_id);
			result.put("optFlag", "Y");
		} catch (Exception e) {
			e.printStackTrace();
			result.put("optFlag", "E");
			result.put("optDesc", "系统错误,请稍后重试!");
		}
		return result;
	}
	
	/**
	 * 查找权限
	 * @param params
	 * @return
	 */
	@RequestMapping("/findRights")
	public List<Map<String, Object>> findRights(@RequestParam Map<String, String> params) {
		return menuService.findAllRights(params);
	}
	
	/**
	 * 添加菜单
	 * @param params
	 * @return
	 */
	@RequestMapping("/addMenu")
	public Map<String, String> addMenu(@RequestParam Map<String, String> params) {
		Map<String, String> result = new HashMap<String, String>();
		try {
			if (null == params.get("right_name")) {
				return null;
			}
			
			menuService.insertMenu(params);
			result.put("optFlag", "Y");
		} catch (Exception e) {
			e.printStackTrace();
			result.put("optFlag", "E");
			result.put("optDesc", "系统错误,请稍后重试!");
		}
		return result;
	}
	
	/**
	 * 查找菜单项
	 * @param right_id
	 * @return
	 */
	@RequestMapping("/find")
	public Map<String, Object> findMenu(String right_id) {
		return menuService.findRightById(right_id);
	}
	/**
	 * 编辑菜单
	 * @param params
	 * @return
	 */
	@RequestMapping("/editMenu")
	public Map<String, String> editMenu(@RequestParam Map<String, String> params) {
		Map<String, String> result = new HashMap<String, String>();
		try {
			if (null == params.get("right_name"))
				return null;
			menuService.updateMenu(params);
			result.put("optFlag", "Y");
		} catch (Exception e) {
			e.printStackTrace();
			result.put("optFlag", "E");
			result.put("optDesc", "系统错误,请稍后重试!");
		}
		return result;
	}
	
	/**
	 * 删除菜单
	 * @param right_id
	 * @return
	 */
	@RequestMapping("/deleteRight")
	public Map<String, String> deleteMenu(String right_id) {
		Map<String, String> result = new HashMap<String, String>();
		try {
			menuService.deleteMenu(right_id);
			result.put("optFlag", "Y");
		} catch (Exception e) {
			e.printStackTrace();
			result.put("optFlag", "E");
			result.put("optDesc", "系统错误,请稍后重试!");
		}
		return result;
	}
	

}
